package com.example.forum.mapper;

import com.example.forum.dto.Pagination;
import com.example.forum.entity.Article;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface ArticleMapper{
    //新增文章
    @Insert("insert into article (user_id,title,content,tag,categoryId,category,status) values (#{article.userId},#{article.title},#{article.content},#{article.tag},#{article.categoryId},#{article.category},#{article.status})")
    @Options(useGeneratedKeys = true,keyProperty ="id")
    Integer insert(@Param("article")Article article);

    @Delete("delete from article where id=#{id}")
    Integer delete(@Param("id")Integer id);

    @Select("select * from article where id=#{id}")
    Article select(@Param("id")Integer id);

    @Select("select * from article where id=#{id}")
    List<Article> selects(@Param("id")Integer id);
    //月榜
    @Select("select * from article where created_at>=#{date} order by viewCount desc")
    List<Article> selectMonth(@Param("date") Date date);

    @Update("UPDATE article SET favorite_count=favorite_count+1 WHERE id=#{id};")
    Integer favNum(@Param("id")Integer id);
    @Update("UPDATE article SET favorite_count=favorite_count-1 WHERE id=#{id};")
    Integer favNumJian(@Param("id")Integer id);

    @Update("UPDATE article SET like_count=like_count+1 WHERE id=#{id};")
    Integer loveNum(@Param("id")Integer id);
    @Update("UPDATE article SET like_count=like_count-1 WHERE id=#{id};")
    Integer loveNumJian(@Param("id")Integer id);
//回复加1
@Update("UPDATE article SET replyCount=replyCount+1 WHERE id=#{id};")
Integer replyNum(@Param("id")Integer id);

//访问加1
    @Update("UPDATE article SET viewCount=viewCount+1 WHERE id=#{id};")
    Integer viewNum(@Param("id")Integer id);
//热门标签
    @Select("select  tag from article where tag is not null order by viewCount desc ")
    List<String> findTag();

    @Select("select * from article where user_id=#{id} order by id desc")
    List <Article>  mypost(@Param("id")Integer id);

    @Select("select count(*) from article ")
    Integer num();
    @Select("<script>"+
            "select * from article "+
            "<where>"+
            "<if test='pagination.title!=null'>and title like concat('%',#{pagination.title},'%')</if>"+
            "<if test='pagination.userId!=null'>and user_id=#{pagination.userId}</if>"+
            "<if test='pagination.categoryId!=null'>and categoryId =#{pagination.categoryId}</if>"+
            "<if test='pagination.status!=null'>and status=#{pagination.status}</if>"+
            "<if test='pagination.tag!=null'>and tag like concat('%',#{pagination.tag},'%')</if>"+
    "</where>"+
            "order by id desc "+
            " limit #{pagination.offset},#{pagination.limit}"+
            "</script>"
    )
    List<Article>findArticles(@Param("pagination")Pagination pagination);

/**
根据条件查找文章
 @Select("<script>"+
 "SELECT * FROM book"+
 "<where>"+
 "<if test='book.name!=null'>AND name LIKE CONCAT('%',#{book.name},'%')</if>"+
 "<if test='book.isbn!=null'>AND isbn=#{book.isbn}</if>"+
 "</where>"+
 "ORDER BY id ASC "+
 "LIMIT #{pagination.offset},#{pagination.limit}"+
 "</script>")
 List<Book>findBooks(@Param("book")Book book, @Param("pagination") Pagination pagination);

  *     protected int page;//当前页面
  *     protected int limit;//页面条数
  *     protected long total;//总数
  *     protected Integer category;//种类id
  *     protected Date createdBegin;//开始时间
  *     protected Date createdEnd;//结束时间
  *     protected Integer recommend;
  *     protected Integer status;//状态（个人可见或人类可见）
  *     protected String tag;//标签
  *     protected String title;//标题
  *     protected Integer userId;//用户id
  *     protected String username;//用户名

 *
 */
}
